package com.power.sifa.correction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.power.sifa.correction.model.Apl_condeploy_cert;
import com.power.sifa.correction.model.pageInfo.Apl_condeploy_certPageInfo;
import com.power.sifa.correction.api.IApl_condeploy_certFeign;
import com.power.sifa.correction.service.Apl_condeploy_certService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   社区矫正人员布控主表控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_condeploy_certController",tags={"社区矫正人员布控主表"})
public class Apl_condeploy_certController implements IApl_condeploy_certFeign{

    @Resource
    private Apl_condeploy_certService apl_condeploy_certService;


    @Override
    @ApiOperation("查询社区矫正人员布控主表分页列表")
    @SaCheckPermission("apl_condeploy_cert_list")
    public ResponseDTO list(Apl_condeploy_certPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_condeploy_cert> page = this.apl_condeploy_certService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_condeploy_cert apl_condeploy_cert : page) {
                apl_condeploy_cert.geneText();
                list.add(apl_condeploy_cert);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看社区矫正人员布控主表")
    @SaCheckPermission("apl_condeploy_cert_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_condeploy_cert apl_condeploy_cert  = apl_condeploy_certService.getById(id);
        if (apl_condeploy_cert != null) {
            apl_condeploy_cert.geneText();
        }
        return ResponseDTO.succData(apl_condeploy_cert);
    }

    @Override
    @ApiOperation("删除社区矫正人员布控主表")
    @SaCheckPermission("apl_condeploy_cert_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_condeploy_certService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改社区矫正人员布控主表")
    @SaCheckPermission("apl_condeploy_cert_save")
    public ResponseDTO save(Apl_condeploy_cert apl_condeploy_cert) {
        Assert.notNull(apl_condeploy_cert, "apl_condeploy_cert不能为空");
        apl_condeploy_certService.saveOrUpdate(apl_condeploy_cert);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="批量删除社区矫正人员布控主表")
    @SaCheckPermission("apl_condeploy_cert_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<String> list = Arrays.asList(ids.split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
        apl_condeploy_certService.removeByIds(list);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_condeploy_cert_list")
    public void output(HttpServletResponse response, Apl_condeploy_certPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "社区矫正人员布控主表";
        String[] heads = {"序号","禁止令内容","禁止期限起止日","事实及依据","县级意见","地市意见","备注","县级申报人id","县级申报人名称","县级申报时间","地市审批人id","地市审批人名称","地市审批时间"};
        String[] fields = {"id","prohibit","startdate","fact","countymsg","citymsg","remark","countyuserid","countyusername","countydate","cityuserid","cityusername","citydate"};
        Page<Apl_condeploy_cert> page = this.apl_condeploy_certService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }

    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "社区矫正人员布控主表";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","禁止令内容","禁止期限起止日","事实及依据","县级意见","地市意见","备注","县级申报人id","县级申报人名称","县级申报时间","地市审批人id","地市审批人名称","地市审批时间"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }

	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","prohibit","startdate","fact","countymsg","citymsg","remark","countyuserid","countyusername","countydate","cityuserid","cityusername","citydate"};
        List<Apl_condeploy_cert> list = (List<Apl_condeploy_cert>)ExcelUtil.Import(fields, file, Apl_condeploy_cert.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_condeploy_certService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

}
